package com.android.functionmanagement.service;

import com.android.functionmanagement.globle.ConstantGloble;
import com.android.functionmanagement.ui.SosActivity;
import com.android.internal.telephony.Call;
import com.android.internal.telephony.CallManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class CallService extends Service {
	private static final String TAG = "DHYCO";
	private static final int MSG_PHONE_STATE_CHANGED = 0x1000;
	private static final int MSG_PHONE_CALL_NUMBER = 0x1100;
	private static final int MSG_START_SOS_LOCATION = 0x1200;
	private Handler mHandler = new Handler() {
		public void dispatchMessage(android.os.Message msg) {

			switch (msg.what) {
			case MSG_PHONE_STATE_CHANGED:
				updatePhoneSateChange();
				break;
			case MSG_PHONE_CALL_NUMBER:
				break;
			case MSG_START_SOS_LOCATION:
				break;
			default:
				break;
			}
		};
	};

	@Override
	public IBinder onBind(Intent intent) {
		// TODO: Return the communication channel to the service.
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Service#onCreate()
	 */
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	}

	private boolean mHasRequested = false;
	private boolean mNeedExit = false;
	private int mNumIndex = -1;
	private int mNumLen = 0;

	private void updatePhoneSateChange() {
		CallManager mCM = CallManager.getInstance();
		Call fgCall = mCM.getActiveFgCall();
		if (mCM.hasActiveRingingCall()) {
			fgCall = mCM.getFirstActiveRingingCall();
		}
		final Call.State state = fgCall.getState();
		switch (state) {
		case IDLE:
			mNeedExit |= (!mHasRequested && (mNumIndex == mNumLen * 2 - 1) && mNumIndex > -1);
			if (mNeedExit) {
				Intent in = new Intent(this, AlertService.class);
				in.putExtra(ConstantGloble.ALARMACTION,
						ConstantGloble.SOS_REQUEST_LOCATION);
				startService(in);
				mNumIndex = -1;
				mNumLen = 0;
				mNeedExit = false;
				Log.i(TAG, "dhyco,,,,,IDLE ..*&^&...SOS_REQUEST_LOCATION");
				stopSelf();
			}
			break;
		case ACTIVE:// 去电接通
			Intent intent = new Intent(this, SosActivity.class);
			intent.putExtra("SOS_ACTIVED", true);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
					| Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
			startActivity(intent);
			Log.i(TAG, "dhyco,,,,,ACTIVE ..");
			mNeedExit = true;
			if (mHasRequested){
				mNumIndex = -1;
				mNumLen = 0;
				mNeedExit = false;
				stopSelf();
			}
			break;
		case HOLDING:
			break;
		// case WAITING:
		case INCOMING:
			break;
		// case ALERTING:
		case DIALING:
			if (!mHasRequested && isWifiConnected() && mNumIndex > -1
					&& mNumLen > 0) {
				mHasRequested = true;
//				Intent in = new Intent(this, AlertService.class);
//				in.putExtra(ConstantGloble.ALARMACTION,
//						ConstantGloble.SOS_REQUEST_LOCATION);
//				startService(in);
				mNumIndex = -1;
				mNumLen = 0;
			}
			break;
		default:
			break;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Service#onStartCommand(android.content.Intent, int, int)
	 */
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		if (null == intent)
			return START_STICKY;
		mNumIndex = intent.getIntExtra("NUMBER_INDEX", -1);
		mNumLen = intent.getIntExtra("NUMBER_LEN", 0);
		if (mNumIndex > -1 && mNumLen > 0)
			CallManager.getInstance().registerForPreciseCallStateChanged(
					mHandler, MSG_PHONE_STATE_CHANGED, null);
		return START_STICKY;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Service#onDestroy()
	 */
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		CallManager.getInstance()
				.unregisterForPreciseCallStateChanged(mHandler);
	}

	private boolean isWifiConnected() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		State wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();
		NetworkInfo info = cm.getActiveNetworkInfo();
		return (wifiState != null && State.CONNECTED == wifiState)
				&& (info != null && info.isConnected() && info.isAvailable());
	}

}
